package com.example.airoles.controller;

import com.example.airoles.service.ConversationAnalyticsService;
import com.example.airoles.service.ConversationSessionService;
import com.example.airoles.dto.ConversationSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/api/analytics")
public class AnalyticsController {
    
    @Autowired
    private ConversationAnalyticsService analyticsService;
    
    @Autowired
    private ConversationSessionService sessionService;
    
    @GetMapping("/conversation/{sessionId}")
    public ResponseEntity<ConversationAnalyticsService.ConversationInsights> analyzeConversation(
            @PathVariable String sessionId) {
        ConversationSession session = sessionService.getSession(sessionId);
        if (session == null) {
            return ResponseEntity.notFound().build();
        }
        
        ConversationAnalyticsService.ConversationInsights insights = 
            analyticsService.analyzeConversation(session);
        return ResponseEntity.ok(insights);
    }
    
    @GetMapping("/user/{userId}")
    public ResponseEntity<ConversationAnalyticsService.UserConversationSummary> getUserSummary(
            @PathVariable String userId,
            @RequestParam(defaultValue = "30") int days) {
        ConversationAnalyticsService.UserConversationSummary summary = 
            analyticsService.getUserConversationSummary(userId, days);
        return ResponseEntity.ok(summary);
    }
    
    @GetMapping("/system")
    public ResponseEntity<ConversationAnalyticsService.SystemAnalytics> getSystemAnalytics() {
        ConversationAnalyticsService.SystemAnalytics analytics = 
            analyticsService.getSystemAnalytics();
        return ResponseEntity.ok(analytics);
    }
    
    @GetMapping("/llm-provider")
    public ResponseEntity<Map<String, Object>> getLlmProviderInfo() {
        Map<String, Object> info = new HashMap<>();
        
        // Using Qiniu AI as the only LLM provider
        try {
            info.put("provider", "qiniu");
            info.put("status", "active");
            info.put("description", "Using Qiniu AI - 七牛云AI大模型推理服务");
            info.put("model", "qwen-plus");
        } catch (Exception e) {
            info.put("provider", "qiniu");
            info.put("status", "error");
            info.put("error", e.getMessage());
        }
        
        return ResponseEntity.ok(info);
    }
}